<?php
namespace Admin\Controller;

use Util\AccessController;

class PostageController extends AccessController {

    public function Index() {
        $LS = Service('M','postage');
        $DATA = $LS->query();
        $this->DATA = $DATA;
        $this->display();
    }
    public function Add(){
        $LS = Service('M','postage');
        $OData = $LS->find(array('STATE'=>1));
        if($OData && $_POST['STATE']){
           $this->error('已经有正在启用的运费模板，请确定需要哪个?');
        }else{
            $_POST['CREATE_TIME'] = date('Y-m-d H:i:s');
            if($_POST['ID']){
                $where['ID']=$_POST['ID'];
                $OK = $LS->update($where,$_POST);
            }else{
                $OK = $LS->Add($_POST);
            }
            $OK ?$this->success('成功'):$this->error('失败');
        }

    }

    //包邮的书籍列表
    public function Bookindex(){
        $files = C('ADMIN_BOOK_LS').',ts_by_book.ID as by_book_id';
        $LS = Service('M','by_book');
        $order = 'ts_book.SORT desc';
        $DATA = $LS->join(null,'ts_book on ts_by_book.BOOK_ID = ts_book.ID',$files,null,$order);
        $this->DATA = $DATA;
        $this->display();
    }
    /*
     * 添加包邮书籍列表
     */
    public function AddBYBook(){
        $LS = Service('M','by_book');
        $save['BOOK_ID'] = $_POST['BOOK_ID'];
        $OK = $LS->Add($save);
        echo $OK;
    }
    /*
     * 删除包邮书籍
     */
    public function deleteByBook(){
        $LS = Service('M','by_book');
        $where['ID'] = $_POST['ID'];
        $OK = $LS->Del($where);
        echo $OK;
    }

    /*
     * 特殊区域邮费
     */
    public function TsAddress(){
        $LS = Service('M','area');
        $where['level'] =1;
        $area = $LS->query($where);
        $this->area = $area;

        $LS1 = Service('M','postage_adderss');
        $DATA = $LS1->query();
        $this->DATA =$DATA;
        $this->display();
    }
    public function AddAdderss(){
        $LS = Service('M','postage_adderss');
        $OK = $LS->Add($_POST);
        $OK?$this->SUCCESS('添加成功'):$this->ERROR('添加失败');
    }
    /*
   * 删除包邮书籍
   */
    public function deleteAdderss(){
        $LS = Service('M','postage_adderss');
        $where['ID'] = $_POST['ID'];
        $OK = $LS->Del($where);
        echo $OK;
    }
}